package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.InventoryLine;
import com.zyd.shiro.business.service.PakInventoryLineService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakInventoryLine;
import com.zyd.shiro.persistence.mapper.PakInventoryLineMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakInventoryLineServiceImpl implements PakInventoryLineService {

    @Autowired
    private PakInventoryLineMapper pakInventoryLineMapper;

    @Override
    public InventoryLine insert(InventoryLine inventoryLine) {
        Assert.notNull(inventoryLine, "对象不可为空！");
        inventoryLine.getPakInventoryLine().setUpdateTime(new Date());
        inventoryLine.getPakInventoryLine().setCreateTime(new Date());
        pakInventoryLineMapper.insertSelective(inventoryLine.getPakInventoryLine());
        return inventoryLine;
    }

    @Override
    public void insertList(List<InventoryLine> inventoryLines) {
            for(InventoryLine inventoryLine:inventoryLines){
               inventoryLine.getPakInventoryLine().setUpdateTime(new Date());
               inventoryLine.getPakInventoryLine().setCreateTime(new Date());
               pakInventoryLineMapper.insertSelective(inventoryLine.getPakInventoryLine());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakInventoryLineMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(InventoryLine inventoryLine) {
        Assert.notNull(inventoryLine, "对象不可为空！");
        inventoryLine.setUpdateTime(new Date());
        return pakInventoryLineMapper.updateByPrimaryKey(inventoryLine.getPakInventoryLine()) > 0;
    }

    @Override
    public boolean updateSelective(InventoryLine inventoryLine) {
        Assert.notNull(inventoryLine, "对象不可为空！");
        inventoryLine.setUpdateTime(new Date());
        return pakInventoryLineMapper.updateByPrimaryKeySelective(inventoryLine.getPakInventoryLine()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public InventoryLine getByPrimaryKey(Long primaryKey) {
        PakInventoryLine pakInventoryLine = pakInventoryLineMapper.selectByPrimaryKey(primaryKey);
        return null == pakInventoryLine ? null : new InventoryLine(pakInventoryLine);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public InventoryLine getOneByEntity(InventoryLine test) {
        Assert.notNull(test, "对象不可为空！");
        PakInventoryLine pakInventoryLine = pakInventoryLineMapper.selectOne(test.getPakInventoryLine());
        return null == pakInventoryLine ? null : new InventoryLine(pakInventoryLine);
    }

    @Override
    public List<InventoryLine> listAll() {
        List<PakInventoryLine> pakInventoryLines = pakInventoryLineMapper.selectAll();
        if (CollectionUtils.isEmpty(pakInventoryLines)) {
            return null;
        }
        List<InventoryLine> inventoryLines = new ArrayList<>();
        for (PakInventoryLine pakInventoryLine : pakInventoryLines) {
            inventoryLines.add(new InventoryLine(pakInventoryLine));
        }
        return inventoryLines;
    }

    @Override
    public List<InventoryLine> listByEntity(InventoryLine entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<InventoryLine> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakInventoryLine> pakInventoryLines = pakInventoryLineMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakInventoryLines)) {
            return null;
        }
        List<InventoryLine> inventoryLines = new ArrayList<>();
        for (PakInventoryLine pakInventoryLine : pakInventoryLines) {
            inventoryLines.add(new InventoryLine(pakInventoryLine));
        }
        PageInfo bean = new PageInfo<PakInventoryLine>(pakInventoryLines);
        bean.setList(inventoryLines);
        return bean;
    }
@Override
    public void increase(PakInventoryLine pakInventoryLine) {
        pakInventoryLineMapper.insert(pakInventoryLine);
    }




}
